package com.queen.restaurant.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.queen.restaurant.domain.CourseType;
import com.queen.restaurant.domain.Restaurant;
import com.queen.restaurant.domain.Table;
import com.queen.restaurant.service.CourseTypeService;
import com.queen.restaurant.service.RestaurantService;
import com.queen.restaurant.service.TableService;

/**
 * 菜品类别操作Controller
 * 
 * @author Queen
 * Description:
 * <br/>Program Name:QueenRestaurant
 * <br/>Date:2013.07.11
 * */

@Controller
public class CourseTypeController {

	@Autowired
	private CourseTypeService courseTypeService;
	@Autowired
	private RestaurantService restaurantService;
	@Autowired
	private TableService tableService;
	// 获取菜品类别页面
	@RequestMapping(value = "/restaurant/course_type")
	public String getCourseTypePage() {
		return "restaurant/restaurant_course_type";
	}

	// 登陆餐厅的所有菜品类别
	@RequestMapping(value = "/restaurant/course_type/list")
	@ResponseBody
	public List<CourseType> getCourseTypeList(HttpSession session) {
		Restaurant restaurant = (Restaurant) session.getAttribute("restaurant");
		return courseTypeService.getCourseTypeByRestaurantId(restaurant.getId());
	}
	
	// 新增菜品类别
	@RequestMapping(value = "/restaurant/course_type/create", method = RequestMethod.POST)
	@ResponseBody
	public List<CourseType> createCourseType(
			@RequestBody CourseType courseType,
			HttpSession session){
		Restaurant restaurant = (Restaurant) session.getAttribute("restaurant");
		courseType.setRestaurant(restaurant);
		courseTypeService.createCourseType(courseType);
		return courseTypeService.getCourseTypeByRestaurantId(restaurant.getId());
	}
	
	// 删除菜品类别
	@RequestMapping(value = "/restaurant/course_type/delete", method = RequestMethod.POST)
	@ResponseBody
	public List<CourseType> deleteCourseType(
			@RequestBody String courseTypeId,
			HttpSession session){
		courseTypeService.deleteCourseTypeById(Integer.parseInt(courseTypeId));
		Restaurant restaurant = (Restaurant) session.getAttribute("restaurant");
		return courseTypeService.getCourseTypeByRestaurantId(restaurant.getId());
	}
	
	// 修改菜品类别
	@RequestMapping(value = "/restaurant/course_type/update", method = RequestMethod.POST)
	@ResponseBody
	public List<CourseType> updateCourseType(
			@RequestBody CourseType courseType,
			HttpSession session){
		courseTypeService.updateCourseType(courseType);
		Restaurant restaurant = (Restaurant) session.getAttribute("restaurant");
		return courseTypeService.getCourseTypeByRestaurantId(restaurant.getId());
	}
	
//	//根据餐桌id获取对应餐厅的全部菜品
//	@RequestMapping(value = "/course_type/get_list", method = RequestMethod.POST)
//	@ResponseBody
//	public Map<String, Object> getCourseTypeByTableId(
//			@RequestParam("tableId")String tableId){
//		Map<String, Object> resultMap = new HashMap<String, Object>();
//		String result = "fail";
//		Restaurant restaurant = null;
//		List<CourseType> courseTypeList = null;
//		Table table = tableService.getTableById(tableId);
//		if (table != null) {
//			restaurant = restaurantService.getRestaurantById(table.getRestaurant().getId());
//			if (restaurant != null) {
//				courseTypeList = restaurant.getCourseTypes();
//				result = "success";
//			}
//		}
//		resultMap.put("result", result);
//		resultMap.put("courseTypeList", courseTypeList);
//		return resultMap;
//	}
	//根据餐桌id获取对应餐厅的全部菜品
	@RequestMapping(value = "/course_type/get_list", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> getCourseTypeByRestaurantId(
			@RequestParam("restaurantId")String restaurantId){
		Map<String, Object> resultMap = new HashMap<String, Object>();
		String result = "fail";
		Restaurant restaurant = restaurantService.getRestaurantById(restaurantId);
		List<CourseType> courseTypeList = null;
		if (restaurant != null) {
			courseTypeList = restaurant.getCourseTypes();
			result = "success";
		}
		resultMap.put("result", result);
		resultMap.put("courseTypeList", courseTypeList);
		return resultMap;
	}
}
